//
//  SearchGongLvCollectionViewCell.m
//  礼物说
//
//  Created by dllo on 16/4/29.
//  Copyright © 2016年 sunzhichao. All rights reserved.
//

#import "SearchGongLvCollectionViewCell.h"

@interface SearchGongLvCollectionViewCell ()<UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) NSMutableArray * bigDataArr;
@property (nonatomic, assign) NSInteger n;

@end


@implementation SearchGongLvCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createCollectionView];
    }
    return self;
}
- (void)dealloc{
    [_searchStr release];
    [_bigDataArr release];
    [_gongLvArr release];
    [_collectionView release];
    [super dealloc];
}

- (void)setGongLvArr:(NSMutableArray *)gongLvArr{
    if (_gongLvArr != gongLvArr) {
        [_gongLvArr release];
        _gongLvArr = [gongLvArr retain];
    }
    self.bigDataArr = [NSMutableArray arrayWithArray:_gongLvArr];
}

#pragma mark - 创建 CollectionView
- (void)createCollectionView{
    
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 1;
    layout.minimumLineSpacing = 1;
    layout.itemSize = CGSizeMake(kWIDTHS(375) - kWIDTHS(5), kWIDTHS(150));
    layout.sectionInset = UIEdgeInsetsMake(kWIDTHS(5), kWIDTHS(5), kWIDTHS(5), kWIDTHS(5));
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    self.collectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 0, kWIDTHS(375), kHEIGHTS(667) - 64 - kHEIGHTS(40)) collectionViewLayout:layout];
//    self.collectionView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_collectionView];
    [_collectionView release];
    [layout release];
    
    
    _collectionView.bounces = YES;/**<取消回弹 */
    _collectionView.pagingEnabled = NO;
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    
    [_collectionView registerClass:[ContentCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    
    [self createMJBfresh];
}

#pragma mark - 刷新
- (void)createMJBfresh{
    
    _collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"下拉刷新" object:nil userInfo:@{@"judge" : @"gongLv", @"collection" : _collectionView}];
    }];
    self.n = 0;
    _collectionView.mj_footer = [MJRefreshBackFooter footerWithRefreshingBlock:^{
        
        _n = _n + 20;
        NSString * str = [NSString stringWithFormat:@"%ld", (long)_n];
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"上拉加载" object:nil userInfo:@{@"judge" : @"gongLv" , @"page" : str , @"collection" : _collectionView }];
    }];
    
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return _bigDataArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    ContentCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    cell.gongLvContentModel = _bigDataArr[indexPath.row];
    
    GongLvContentModel * model = _bigDataArr[indexPath.row];
    
    
#pragma mark 设置搜索结果是高亮状态
    //     搜索的关键字
    NSString * keyWord = _searchStr;
    //     搜索结果的字符串
    NSString *resultStr = model.title;
    
    //     设置标签文字
    NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] initWithString:resultStr];
    //
    //     设置标红的位置和长度
    NSRange range = [resultStr rangeOfString:keyWord];
    //
    //     设置标签文字的属性
    [attrituteString setAttributes:@{NSForegroundColorAttributeName:[UIColor redColor], NSFontAttributeName:[UIFont systemFontOfSize:15]} range:range];
    //
    //     将设置好的高亮状态显示在Lable上
    cell.titleLable.attributedText = attrituteString;
    
    return cell;
}

#pragma mark - collectionView点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    GongLvXiangQingViewController * gongLvVc = [[GongLvXiangQingViewController alloc] init];
    
    
    UIViewController * viewController = [self viewController];
    
    
    GongLvContentModel * model = _bigDataArr[indexPath.row];
    gongLvVc.number = model.number;
    
    [viewController.navigationController pushViewController: gongLvVc animated:YES];
    [gongLvVc release];
    
}
//找到跟视图
- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}


@end
